var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/baseComponent/select.tsx
var select_exports = {};
__export(select_exports, {
  MultipleSelect: () => MultipleSelect,
  Select: () => Select
});
module.exports = __toCommonJS(select_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_functionalComponent = require("../functionalComponent");
var import_util = require("../util");
var import_combobox = require("./combobox");
var SelectComp = (0, import_util.compHoc)(
  class extends import_util.IUPComponent {
    constructor(props) {
      super(props);
      this._cancel = false;
      this.onSelectChange = (value, option) => {
        if (this.props.labelInValue !== false) {
          if (import_util.iup.isArray(value)) {
            value.forEach((v, index) => {
              var _a;
              if (!v.origin) {
                v.origin = { ...((_a = option == null ? void 0 : option[index]) == null ? void 0 : _a["data-origin"]) || (option == null ? void 0 : option[index]) || v };
              }
            });
          } else if (value) {
            if (!value.origin) {
              value.origin = { ...(option == null ? void 0 : option["data-origin"]) || option || value };
            }
          }
        }
        if (this.props.hasOwnProperty("value")) {
          this.state.value = this.formatValue(value);
          this.props.onChange && this.props.onChange(value, option);
        } else {
          this.setValue(value, option);
        }
        this.innerNotify([value], "onChange").then();
      };
      this.getTooltipRender = (value, tooltip) => () => {
        if (import_util.iup.isFunction(tooltip)) {
          return tooltip(value);
        }
        return tooltip;
      };
      this.state = {
        data: import_util.iup.isArray(props.data) ? props.data : void 0,
        request: props.request,
        loading: !!props.request,
        value: props.defaultValue
      };
    }
    _getLabel(value, data) {
      var _a, _b, _c, _d;
      const { valueField = "value", labelField = "label" } = this.props;
      const options = data || this.props.options || ((_a = this.state) == null ? void 0 : _a.data);
      const label = (_b = options == null ? void 0 : options.find((option) => option[valueField] == value)) == null ? void 0 : _b[labelField];
      if (import_util.iup.isNullOrEmpty(label, "")) {
        const ps = ((_d = (_c = this.props.children) == null ? void 0 : _c.find(({ props }) => props.value == value)) == null ? void 0 : _d.props) || {};
        return ps[labelField] ?? ps.children;
      }
      return label;
    }
    _updateLabelInValue(value, data) {
      var _a;
      if (this.props.labelInValue === false) {
        return value;
      }
      const options = data || this.props.options || ((_a = this.state) == null ? void 0 : _a.data);
      const labelInValue = import_util.iup.isObject(value) ? { ...value } : { value };
      const { valueField = "value" } = this.props;
      labelInValue.label = this._getLabel(labelInValue.value, data) ?? (labelInValue.hasOwnProperty("label") ? labelInValue.label : labelInValue.value);
      if (options && !labelInValue.origin) {
        labelInValue.origin = { ...options.find((option) => option[valueField] == value) };
      }
      if (labelInValue.value === void 0 || !this.props.allowNull && labelInValue.value === null) {
        return void 0;
      }
      return labelInValue;
    }
    formatValue(value, data) {
      const multiple = this.isMultiple();
      if (multiple && !import_util.iup.isArray(value)) {
        value = import_util.iup.isNullOrEmpty(value) ? [] : [value];
      }
      return multiple ? value == null ? void 0 : value.map((v) => this._updateLabelInValue(v, data)).filter((f) => f !== void 0) : this._updateLabelInValue(value, data);
    }
    isMultiple() {
      return this.props.multiple || this.props.mode === "multiple";
    }
    static getDerivedStateFromProps(nextProps, prevState) {
      if (import_util.iup.isArray(nextProps.data)) {
        return import_util.IUPComponent.propsToState(nextProps, prevState, ["value", "data"]);
      }
      return import_util.IUPComponent.propsToState(nextProps, prevState, ["value"]);
    }
    componentDidMount() {
      if (this.state.request) {
        this.requestData().then();
      }
    }
    componentDidUpdate(prevProps) {
      if (prevProps.request !== this.props.request && this.props.request) {
        this.requestData().then();
      }
    }
    async requestData(params = {}) {
      const { request } = this.props;
      this.setState({ loading: true });
      const res = await request(params);
      if (!this._cancel) {
        this.setData(import_util.iup.parseJson(res));
      }
    }
    setData(data) {
      if (data !== this.state.data) {
        const value = this.formatValue(this.state.value, data);
        const oldValue = this.state.value;
        this.setState({ data, value, loading: false }, () => {
          var _a, _b;
          oldValue !== value && ((_b = (_a = this.props).onChange) == null ? void 0 : _b.call(_a, value, data));
        });
      } else if (this.state.loading) {
        this.setState({ loading: false });
      }
    }
    componentWillUnmount() {
      this._cancel = true;
    }
    setValue(val, option, cb) {
      const value = this.formatValue(val);
      this.setState(
        (prev) => ({
          ...prev,
          value
        }),
        () => {
          this.props.onChange && this.props.onChange(value, option);
          cb && cb(value, option);
        }
      );
    }
    getValue(joinStr) {
      const value = this.formatValue(this.state.value);
      const val = (v) => {
        if ((v == null ? void 0 : v.value) === void 0) {
          return v;
        }
        return v.value;
      };
      if (this.isMultiple()) {
        const tmp = value == null ? void 0 : value.map(val);
        return joinStr ? tmp.join(joinStr) : tmp;
      }
      return val(value);
    }
    getRawValue() {
      return this.formatValue(this.state.value);
    }
    getOrigin() {
      var _a, _b;
      if (!this.state.value) {
        return void 0;
      }
      if (this.isMultiple()) {
        const originArr = [];
        (_b = (_a = this.state.value).forEach) == null ? void 0 : _b.call(_a, (item) => {
          if (item && item.hasOwnProperty("value")) {
            item.origin && originArr.push(item.origin);
          }
        });
        return originArr;
      }
      return this.state.value.origin;
    }
    getText(joinStr) {
      const value = this.formatValue(this.state.value);
      if (this.isMultiple()) {
        const tmp = value == null ? void 0 : value.map((v) => import_util.iup.isObject(v) ? v.label || this._getLabel(v.value) : this._getLabel(v));
        return joinStr ? tmp.join(joinStr) : tmp;
      }
      return (value == null ? void 0 : value.label) ?? this._getLabel(this.getValue());
    }
    render() {
      const {
        request,
        value,
        open,
        options,
        allowClear = true,
        allowNull,
        labelInValue = true,
        optionLabelProp,
        labelField = "label",
        valueField = "value",
        fitHeight = false,
        tooltip,
        multiple,
        data: propsData,
        observer,
        isTree,
        dropdownStyle,
        ...others
      } = this.props;
      const { loading, data } = this.state;
      const prop = { value: this.formatValue(this.state.value, data) };
      if (fitHeight) {
        prop.className = import_util.iup.classNames(others.className, "fit-height");
      }
      if (open !== void 0) {
        prop.open = loading ? false : open;
      }
      if (allowClear !== void 0) {
        prop.allowClear = loading ? false : allowClear;
      }
      if (multiple && !isTree) {
        prop.mode = "multiple";
      } else {
        prop.multiple = multiple;
      }
      if (others.disabled) {
        prop.suffixIcon = null;
        prop.allowClear = false;
        prop.placeholder = "";
        if (!others.onMouseDownCapture) {
          prop.onMouseDownCapture = (e) => e.stopPropagation();
        }
      }
      if (tooltip) {
        prop.className = import_util.iup.classNames(others.className, "disable-title");
      }
      const renderSelect = () => {
        var _a;
        const dataOptions = loading ? [] : (_a = data || options) == null ? void 0 : _a.map((item) => {
          return { ...item, value: item[valueField], label: item[labelField], "data-origin": item };
        });
        if (isTree) {
          return /* @__PURE__ */ import_react.default.createElement(
            import_antd.TreeSelect,
            {
              treeDefaultExpandAll: true,
              ref: this.outRef,
              labelInValue,
              treeData: dataOptions,
              ...others,
              ...prop,
              virtual: true,
              dropdownStyle: { display: loading ? "none" : dropdownStyle },
              onChange: this.onSelectChange
            }
          );
        }
        return /* @__PURE__ */ import_react.default.createElement(
          import_antd.Select,
          {
            ref: this.outRef,
            loading,
            labelInValue,
            options: dataOptions,
            ...others,
            ...prop,
            optionLabelProp,
            dropdownStyle: { display: loading ? "none" : dropdownStyle },
            onChange: this.onSelectChange
          }
        );
      };
      return tooltip ? /* @__PURE__ */ import_react.default.createElement(import_functionalComponent.Tooltip, { title: this.getTooltipRender(prop.value, tooltip), titleDep: prop.value }, renderSelect()) : renderSelect();
    }
  },
  "Select"
);
var Select = class extends import_react.default.Component {
  constructor() {
    super(...arguments);
    this.outRef = import_react.default.createRef();
  }
  getApi() {
    var _a;
    return (_a = this.outRef.current) == null ? void 0 : _a.getApi();
  }
  setReadOnly(...args) {
    var _a, _b;
    (_b = (_a = this.getApi()) == null ? void 0 : _a.setReadOnly) == null ? void 0 : _b.call(_a, ...args);
  }
  on(...args) {
    var _a, _b;
    (_b = (_a = this.getApi()) == null ? void 0 : _a.on) == null ? void 0 : _b.call(_a, ...args);
  }
  render() {
    const { editable = false, displayField, ...others } = this.props;
    let ds = others.options || others.data;
    if (editable && ds) {
      const {
        options,
        data,
        multiple,
        request,
        mode,
        valueField = "value",
        labelField = "label",
        fitHeight,
        allowNull,
        labelInValue,
        ...cbProps
      } = others;
      if (valueField !== "value" || labelField !== "label") {
        ds = ds.map((d) => ({ value: d[valueField], label: d[labelField] }));
      }
      return /* @__PURE__ */ import_react.default.createElement(import_combobox.Combobox, { ...cbProps, options: ds, ref: this.outRef });
    }
    return /* @__PURE__ */ import_react.default.createElement(SelectComp, { ...others, ref: this.outRef });
  }
};
Select.Option = import_antd.Select.Option;
var MultipleSelect = import_react.default.forwardRef((props, ref) => {
  return /* @__PURE__ */ import_react.default.createElement(Select, { ...props, multiple: true, ref });
});
(0, import_util.registerComponent)({ MultipleSelect }, { multiple: true });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MultipleSelect,
  Select
});
